$(function (){
	searchInit();
	headerNavInit();
	pubInit();
	
	if (typeof g_fwxz_aid != 'undefined'){
		initFwxzRel(g_fwxz_aid);
	}
});

// 出版物初始化
function pubInit()
{
	$('#pub_pre').click(function (){
		pub_index--;
		pubChange();
	});
	$('#pub_next').click(function (){
		pub_index++;
		pubChange();
	});
}

function pubChange()
{
	if (pub_index < 0)
		pub_index = arr_pubs.length-1;
	if (pub_index >= arr_pubs.length)
		pub_index = 0;
	
	var currentInfo = arr_pubs[pub_index];
	$('#pub_href').attr("href", currentInfo['url']);
	$('#pub_img').attr("src", currentInfo['image']);
	$('#pub_name').html(currentInfo['name']);
}

// 搜索初始化
function searchInit()
{	
	$('#asearch').click(function (){
		if ($('#searchname').val() != '' && $('#searchname').val() !='全站搜索')
			location.href=baseUrl + '/index.php?a=search&q=' +encodeURIComponent($('#searchname').val());
		return false;
	});
	$('#searchname').focus(function (){
		if (this.value=='全站搜索'){
			this.value = '';
		}
	}).blur(function (){
		if (this.value == '')
			this.value='全站搜索';
	});
}

// 头部导航初始化
function headerNavInit()
{
	var obj = $('#header_nav_ul > li');
	obj.each(function (i, n){
		$(n).mouseover(function (){
			$(this).find("ul").show();
		})
		.mouseout(function (){
			$(this).find("ul").hide();
		});
	});
}

function toggleHeaderImage(seq)
{
	if (seq >= headerimage_maxindex)
		seq = 1;
	
	var arr = headerimages[seq-1];
	
	var old_link = $('#headerimage_a_' + headerimage_index);
	var new_link = $('#headerimage_a_' + seq);
	var obj = $('#headerimage_p');

	obj.find("img").fadeOut(800, function (){
		$(this).attr("src", arr['image']);
		obj.find("img").fadeIn(600);
	});
	
	var content = "";
	if (arr.url.length < 5){
		content = '<p class="pa">' + arr['title'] + '</p>';
	}else{
		content = '<a href="' +arr['url']+'" target="_blank"><p class="pa">'+arr['title']+'</p></a>';
	}
	$('#head_article_url').html(content);
		
	old_link.removeClass("current");
	new_link.addClass('current');
	
	headerimage_index = seq;
	return false;
}

/**
 * 加入收藏
 */
function addFavorite(url,title) 
{
        try 
        { 
                window.external.addfavorite(url,title); 
        } 
        catch (e) 
        { 
                try 
                { 
                        window.sidebar.addPanel(title, url, ""); 
                } 
                catch (e) 
                { 
                        alert("加入收藏失败，请使用ctrl+d进行添加"); 
                } 
        } 
}

// 访问学者，相关文章
function initFwxzRel(id)
{
	var url = baseUrl +"/index.php?c=fwxz&a=rel";
	$.post(url, {id:id}, function (result){
		if (result.length > 0){
			var html = "";
			html += '<h4>相关新闻</h4>';
			for(var i=0; i<result.length; i++){
				html += '<li><a href="'+result[i].url+'">'+result[i].title+'</a></li>';
			}
			$('#fwxz_rel_list').html(html);
		}
	}, "json");
}